package com.wrench.elevator.base.user.service;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.wrench.elevator.base.user.dao.FlowDao;
import com.wrench.elevator.base.user.domain.Flow;
import com.wrench.elevator.constant.Result;
import com.wrench.elevator.exception.WrenchException;
import com.wrench.elevator.util.WrenchUtil;

/**
 * @Description: FlowService
 * @author yangchenfeng
 * @date Oct 24, 2017 9:53:54 AM
 * @version V1.0
 */
@Component
public class FlowService
{
    private static final Logger logger = LoggerFactory.getLogger(FlowService.class);
    
    @Resource
    private FlowDao dao;
    
    public int addFlow(String flow_type, String flow_money, String user_id, String flow_no, String flow_time)
        throws WrenchException
    {
        // 判空
        if (WrenchUtil.hasNull(flow_type, flow_money, user_id, flow_no, flow_time))
        {
            logger.error("==> param is not found , flow_type = {} ,flow_money = {} , user_id = {} , flow_no = {} , flow_time = {} ",
                flow_type,
                flow_money,
                user_id,
                flow_no,
                flow_time);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
        }
        
        flow_type = flow_type.trim();
        flow_money = flow_money.trim();
        user_id = user_id.trim();
        flow_no = flow_no.trim();
        flow_time = flow_time.trim();
        
        // 判断流水类型
        
        if (!WrenchUtil.isVaildFlowType(flow_type))
        {
            logger.error("==> flow type is not vaild , flow_type = {} ", flow_type);
            throw new WrenchException(Result.FLOW_TYPE_IS_NOT_VAILD_CODE, Result.FLOW_TYPE_IS_NOT_VAILD_MESSAGE);
        }
        
        // 判断流水头与流水类型是否相等
        String flow_no_head = flow_no.substring(0, 2);
        if (!WrenchUtil.stringEquals(flow_no_head, flow_type))
        {
            logger.error("==> flow_no_head is not equals flow_type , flow_no_head = {} , flow_type = {} ",
                flow_no_head,
                flow_type);
            throw new WrenchException(Result.FLOW_NO_HEAD_IS_NOT_EQUALS_FLOW_TYPE_CODE,
                Result.FLOW_NO_HEAD_IS_NOT_EQUALS_FLOW_TYPE_MESSAGE);
        }
        
        // 判断是否金额数字
        if (!WrenchUtil.isMoneyStr(flow_money))
        {
            logger.error("==> string is not money , flow_money = {} ", flow_money);
            throw new WrenchException(Result.STRING_IS_NOT_MONEY_CODE, Result.STRING_IS_NOT_MONEY_MESSAGE);
        }
        
        // 判断是否整数
        if (!WrenchUtil.isInt(user_id))
        {
            logger.error("==> string is not int type , user_id = {} ", user_id);
            throw new WrenchException(Result.STRING_IS_NOT_INT_CODE, Result.STRING_IS_NOT_INT_MESSAGE);
        }
        
        // 判断时间格式是否正确
        
        if (!WrenchUtil.isVaildTime(flow_time))
        {
            logger.error("==> time is not vaild , flow_time = {} ", flow_time);
            throw new WrenchException(Result.TIME_IS_NOT_VAILD_CODE, Result.TIME_IS_NOT_VAILD_MESSAGE);
        }
        
        Flow flow = new Flow();
        flow.setFlow_money(Double.valueOf(flow_money));
        flow.setFlow_no(flow_no);
        flow.setFlow_time(flow_time);
        flow.setFlow_type(flow_type);
        flow.setUser_id(Integer.valueOf(user_id));
        
        int commit = -1;
        
        try
        {
            commit = dao.addFlow(flow);
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            logger.error("==> commit=dao.addFlow(flow) execute error , flow  = {} , error = {}; ", flow, e);
            throw new WrenchException(Result.DB_INSERT_ERROR_CODE, Result.DB_INSERT_ERROR_MESSAGE);
        }
        
        if (commit <= 0)
        {
            logger.error("==> cocommit=dao.addFlow(flow) execute success , but commit <=0 , commit = {} , flow = {} ",
                commit,
                flow);
            throw new WrenchException(Result.CONSUME_ADD_CONSUME_FAIL_CODE, Result.CONSUME_ADD_CONSUME_FAIL_MESSAGE);
        }
        
        logger.debug("==> commit=dao.addFlow(flow) execute success , commit = {} , flow = {} ", commit, flow);
        return commit;
    }
}
